#include <QMouseEvent>
#include <QPainter>
#include <QtDebug>
#include <iostream>
#include "PaintCanvas.h"
#include "MainWindow.h"
 #include "CvxWin32.h"
 #include <QPaintEngine>
CPaintCanvas::CPaintCanvas(OCEANUI::CMainWindowBase* parent):
	CCentralCanvasBase(parent),
	currentImage(NULL),
	tempImage(NULL)
{
     setAttribute(Qt::WA_NoBackground);
	 bDrawing = false;
	 curShape = Ellipse;
	 resize(800,600); 
}

void CPaintCanvas::initData(void* pData)
{
	IplImage* pImg = reinterpret_cast<IplImage*>(pData);
	currentImage = pImg;
	paint(currentImage);
}

void CPaintCanvas::setShape(ShapeType shape)
{
	 curShape = shape;
}

void CPaintCanvas::mousePressEvent( QMouseEvent * event )
{
	bDrawing = true;
	x = event->x();
	y = event->y();
	std::cout<<"Mouse press"<<std::endl;
}

void CPaintCanvas::mouseMoveEvent(QMouseEvent *event)
{
	w = event->x() - x;
	h = event->y() - y;
	std::cout<<event->x()<<" "<<event->y()<<"--"<<"h:"<<h<<std::endl;
	tempImage = currentImage;
	paint(tempImage);
}

void CPaintCanvas::mouseReleaseEvent(QMouseEvent *event)
{
	bDrawing = false;
	currentImage = tempImage;
	paint(currentImage);
	((OCEANUI::CMainWindow*)(this->parentWidget()))->OnImageChanged(currentImage);
     
}

void CPaintCanvas::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	QRect qrc = this->geometry();
	if (bDrawing)
	{
		CvRect rc =cvRect(qrc.x(),qrc.y(),tempImage->width,tempImage->height);
		CvxWin32::DrawToHDC(tempImage,painter.paintEngine()->getDC(),&rc);
	} 
	else if( NULL != currentImage)
	{
		CvRect rc =cvRect(qrc.x(),qrc.y(),currentImage->width,currentImage->height);
		CvxWin32::DrawToHDC(currentImage,painter.paintEngine()->getDC(),&rc);
	}
	
	
	//if (bDrawing)
	//{
	//	painter.drawImage(QPoint(0, 0),tempImage);
	//	resize(QSize(tempImage.size()));
	//}
	//
	//else if (!currentImage.isNull())
	//{
	//	painter.drawImage(QPoint(0, 0),currentImage);
	//	resize(QSize(currentImage.size()));
	//}
	//else
	//{
 //       QBrush brush("Black");
	//	painter.setBrush(brush);
	//	painter.drawRect(rc.x(),rc.y(),this->width(),this->height());
	//}
		//std::cout<<"CPaintCanvas::paintEvent(QPaintEvent *event) called"<<std::endl;
	/*if(bDrawing)
    	painter.drawImage(QPoint(0, 0), tempImage);
	else
    	painter.drawImage(QPoint(0, 0), bufferImage);*/
}

void CPaintCanvas::fontChanged(const QFont& font)
{
	textFont = font;
}

void CPaintCanvas::paint( IplImage* image )
{
 
	if (bDrawing)
	{
		//QPainter painter(&image);
		//painter.drawImage(QPoint(0, 0),image);

		/*painter.setPen(curPen);
		painter.setBrush(curBrush);
		switch(curShape) 
		{
		case Rectangle:
			painter.drawRect(x, y, w, h);
			break;
		case Ellipse:
			painter.drawEllipse(x, y, w, h);
			break;
		case Text:
			QFontMetrics metrics(textFont);
			QRect rect = metrics.boundingRect(textFont.family());
			painter.setFont(textFont);
			painter.translate(x, y);
			painter.scale(w/rect.width(), h/rect.height());
			painter.drawText(0, rect.height(), textFont.family());
			break;
		}	*/
	}
	

	update();
}

void CPaintCanvas::penChanged(QPen& pen)
{
	curPen = pen;
}

void CPaintCanvas::brushChanged(QBrush& brush)
{
	curBrush = brush;
}

void CPaintCanvas::resizeEvent( QResizeEvent *e )
{
	QWidget::resizeEvent(e);
}